﻿using System.Collections.Generic;
using UnityEngine;

namespace TG
{
    public class GameData
    {
        //当前关卡
        public int levelID = 3;

        /// <summary>
        /// 当前活力值
        /// </summary>
        public int vitalityValue;

        /// <summary>
        /// 当前分数
        /// </summary>
        public int score;

        /// <summary>
        /// 倒计时
        /// </summary>
        public int gameTime;

        public int subVitality;

        /// <summary>
        /// 已经吃了的东西
        /// </summary>
        public Dictionary<MapItemType, int> targetDic = new Dictionary<MapItemType, int>();

        public GameConfigData gameConfigData;

        public List<EntityBase> allEntity = new List<EntityBase>();

        public void InitGameConfigData()
        {
            if (MainGame.Instance.curGameConfig.gameType == 0)
            {
                gameConfigData = MainGame.Instance.GameConfig.info[levelID];
                gameTime = gameConfigData.limitTime * 60;
                switch (MainGame.Instance.curGameConfig.difficulty)
                {
                    case 0:
                        subVitality = gameConfigData.easy;
                        break;
                    default:
                        subVitality = gameConfigData.diff;
                        break;
                }
            }
            else
            {
                gameConfigData = null;
                gameTime = MainGame.Instance.curGameConfig.gameTime * 60;
                switch (MainGame.Instance.curGameConfig.difficulty)
                {
                    case 0:
                        subVitality = Default.TrainEasy;
                        break;
                    default:
                        subVitality = Default.TrainDiff;
                        break;
                }
            }

            vitalityValue = Default.MaxVitality;
        }

        public void LossOfVitality()
        {
            AddVitality(-subVitality);
            if (vitalityValue == 0)
            {
                //游戏结束 退出游戏
                MainGame.Instance.ExitScene();
            }
        }

        public void LossOfTime()
        {
            gameTime--;
            if (gameTime <= 0)
            {
                //游戏结束 退出游戏
                MainGame.Instance.ExitScene();
            }
        }

        /// <summary>
        /// 下一关
        /// </summary>
        public void LevelUp()
        {
            levelID++;
            Debug.Log($"升级！！！");
            Reset();
        }

        public void Reset()
        {
            vitalityValue = Default.MaxVitality;
            targetDic.Clear();
            InitGameConfigData();
        }


        public void AddVitality(int vitality)
        {
            vitalityValue += vitality;
            vitalityValue = Mathf.Min(Default.MaxVitality, vitalityValue);
            vitalityValue = Mathf.Max(0, vitalityValue);
        }

        public void AddScore(int score)
        {
            this.score += score;
        }

        public void AddEntity(EntityBase entity)
        {
            allEntity.Add(entity);
        }

        public void RemoveEntity(EntityBase entity)
        {
            allEntity.Remove(entity);
        }

        public void RemoveAllEntity()
        {
            for (int i = allEntity.Count - 1; i >= 0; i--)
            {
                allEntity[i].RemoveSelf();
            }

            allEntity.Clear();
        }

        public void EatEntity(EntityBase entity)
        {
            if (entity.mapItemType < MapItemType.Pumpkin) return;

            if (targetDic.ContainsKey(entity.mapItemType))
                targetDic[entity.mapItemType]++;
            else
                targetDic.Add(entity.mapItemType, 1);

            //加分
            AddScore(entity.score);
            if (entity is VegetableEntity vegetableEntity)
            {
                //恢复活力
                AddVitality(vegetableEntity.vitality);
            }

            if (IsLevelChange())
                GameManager.Instance.ChangeLevel();
        }

        public void EatEntity(MapItemType itemType, int num)
        {
            if (targetDic.ContainsKey(itemType))
                targetDic[itemType] += num;
            else
                targetDic.Add(itemType, num);

            //加分
            int scoreByType = Default.GetScoreByType(itemType);
            AddScore(scoreByType);

            //恢复活力
            int vitalityByType = Default.GetVitalityByType(itemType);
            AddVitality(vitalityByType);

            if (IsLevelChange())
                GameManager.Instance.ChangeLevel();
        }

        private bool IsLevelChange()
        {
            if (MainGame.Instance.curGameConfig.gameType == 1) return false;
            
            var needDic = gameConfigData.GetNeedItems();

            if (targetDic.Count <= 0)
                return false;

            foreach (var need in needDic)
            {
                if (!targetDic.TryGetValue(need.Key, out var haveNum)) return false;
                int needNum = need.Value;
                if (haveNum < needNum)
                    return false;
            }

            return true;
        }
    }
}